import os
ports = []
try:
    for t in os.popen("ps -ef|grep HttpService.py").read().split('\n'):
        try:
            ar = t.split(' ')
            ar = [x.strip() for x in ar if x.strip() != '']
            p = ar[1]
            port = int(p)
            if port in ports:
                continue
            ports.append(port)
        except Exception as e:
            print(str(e))
except Exception as e:
    print(str(e))
try:
    ar = os.popen("lsof -i:8887").read().split('\n')[0].split(' ')
    ar = [x.strip() for x in ar if x.strip() != '']
    port = ar[1]
    port = int(port)
    if port not in ports:
        ports.append(port)
except Exception as e:
    print(str(e))
if len(ports) != 0:
    for port in ports:
        try:
            shellCMD = 'kill {0}'.format(port)
            os.popen(shellCMD)
        except Exception as e:
            print(str(e))
    ports.clear()
